#include "gtkWidgetList.h"

GtkWidgetList *gtkWidgetListInit(void) {
	GtkWidgetList *gtkWidgetList = (GtkWidgetList*)malloc(sizeof(GtkWidgetList));
	gtkWidgetList->gtkWidget = GTK_WIDGET_LIST_NULL;
	gtkWidgetList->next = GTK_WIDGET_LIST_NULL;

	return gtkWidgetList;
}

GtkWidgetList* gtkWidgetListMakeNode(void) {
	GtkWidgetList *gtkWidgetList = gtkWidgetListInit();
	return gtkWidgetList;
}

void insertGtkWidgetListNode(GtkWidgetList **gtkWidgetList, GtkWidgetList *gtkWidgetListNode, int position) {
	int i, flag = 0;
	GtkWidgetList *gwlNode, *gwlAux;
	gwlNode = GTK_WIDGET_LIST_NULL;
	gwlAux = *gtkWidgetList;

	if (gwlAux != GTK_WIDGET_LIST_NULL) {
		for (i = 1; i < position; i++) {
			flag = 1;
			gwlNode = gwlAux;
			if (gwlAux->next == GTK_WIDGET_LIST_NULL) {
				break;
			}
			gwlAux = gwlAux->next;
		}
		if (!flag) {
			gtkWidgetListNode->next = *gtkWidgetList;
			*gtkWidgetList = gtkWidgetListNode;
		} else {
			gwlNode->next = gtkWidgetListNode;;
			if (gwlAux != gwlNode) {
				gwlNode->next = gtkWidgetListNode;
				gtkWidgetListNode->next = gwlAux;
			}
		}
	}
}

void removeGtkWidgetListNode(GtkWidgetList **gtkWidgetList, int position) {
	int i, flag = 0;
	GtkWidgetList *gwlNode, *gwlAux;
	gwlNode = GTK_WIDGET_LIST_NULL;
	gwlAux = *gtkWidgetList;
	if (gwlAux != GTK_WIDGET_LIST_NULL) {
		for (i = 1; i < position; i++) {
			flag = 1;
			gwlNode = gwlAux;
			if (gwlAux->next == GTK_WIDGET_LIST_NULL) {
				break;
			}
			gwlAux = gwlAux->next;
		}
		if (!flag) {
			gwlAux = *gtkWidgetList;
			*gtkWidgetList = (*gtkWidgetList)->next;
			free(gwlAux);
		} else {
			if (gwlAux != gwlNode) {
				gwlNode->next = gwlAux->next;
				free(gwlAux);
			}
		}
	}
}

GtkWidgetList* getGtkWidgetListNode(GtkWidgetList *gtkWidgetList, int position) {
	int i, flag = 0;
	GtkWidgetList *gwlNode, *gwlAux;
	gwlNode = GTK_WIDGET_LIST_NULL;
	gwlAux = gtkWidgetList;
	if (gwlAux != GTK_WIDGET_LIST_NULL) {
		for (i = 1; i < position; i++) {
			flag = 1;
			gwlNode = gwlAux;
			if (gwlAux->next == GTK_WIDGET_LIST_NULL) {
				break;
			}
			gwlAux = gwlAux->next;
		}

	}
	return gwlAux;
}

void gtkWidgetListPrint(GtkWidgetList *gtkWidgetList) {
	GtkWidgetList *gwl;
	for (gwl = gtkWidgetList; gwl != GTK_WIDGET_LIST_NULL; gwl = gwl->next) {
		printf("%d\n", gwl->gtkWidget);
	}
}

/*int main(int argc, char **argv) {
	GtkWidgetList *gtkWidgetList, *gwl;
	gtkWidgetList = gtkWidgetListMakeNode();
	gtkWidgetList->gtkWidget = 111;

	gwl = gtkWidgetListMakeNode();
	gwl->gtkWidget = 222;

	gtkWidgetList->next = gwl;

	gwl = gtkWidgetListMakeNode();
	gwl->gtkWidget = 333;

	gtkWidgetList->next->next = gwl;

	//insertGtkWidgetListNode(gtkWidgetList, gwl, 3);

	gwl = gtkWidgetListMakeNode();
	gwl->gtkWidget = 444;

	insertGtkWidgetListNode(&gtkWidgetList, gwl, atoi(argv[1]));

	gtkWidgetListPrint(gtkWidgetList);
	printf("******************************************************************\n");

	removeGtkWidgetListNode(&gtkWidgetList, atoi(argv[2]));

	gtkWidgetListPrint(gtkWidgetList);
	
	printf("\t\tvalor: %d\n", getGtkWidgetListNode(gtkWidgetList, atoi(argv[3]))->gtkWidget);

}*/
